package com.linkhub.oms.admin.modules.logistic.rules.strategy.impl;

import com.linkhub.oms.admin.modules.logistic.rules.domain.ChannelRuleMatchRequest;
import com.linkhub.oms.admin.modules.logistic.rules.strategy.ChannelRuleMatchStrategy;
import com.linkhub.oms.admin.modules.product.service.LocalProductCalculateService;
import com.linkhub.oms.common.enums.IEnumConvert;
import com.linkhub.oms.common.enums.logistic.WeightLimitEnum;
import com.linkhub.oms.common.util.QtBigDecimalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Description: WeightLimit
 * @Author: echo
 * @Date: 2024/6/1 17:00
 * @Version: 1.0
 */
@Slf4j
@Service
@Order(value = 3)
public class WeightLimit extends BaseChannelRuleMatchStrategy implements ChannelRuleMatchStrategy {

    @Resource
    private LocalProductCalculateService localProductCalculateService;
    @Override
    public boolean match(ChannelRuleMatchRequest request) {
        String sysOrderId = request.getOrderInfoDO().getSysOrderId();

        BigDecimal orderProductWeight = localProductCalculateService.getOrderProductWeightTotal(sysOrderId);
        String channelCountryKey = request.assemblyChannelCountryKey();
        WeightLimitEnum weightLimit = IEnumConvert.valueOf(channelCountryKey, WeightLimitEnum.class);
        if (Objects.isNull(weightLimit)) {
            log.info("WeightLimit skip,  key=[{}]", channelCountryKey);
            return true;
        }

        return QtBigDecimalUtil.lt(orderProductWeight, weightLimit.getG());
    }
}
